#pragma once

#include "IWriter.hpp"
#include <string>
#include "IWriteStrategy.hpp"
#include "NormalSrategy.hpp"
#include "LazyStrategy.hpp"
#include <iostream>

class CFileWriter : public IWriter{
public:
    CFileWriter(const std::string& filename,StrategyTag tag = StrategyTag::NORMAL):
    m_filename(filename)
    {
        if(tag == StrategyTag::NORMAL){
            m_pStrategy = new NormalStrategy();
        }else if(tag == StrategyTag::LAZY){
            m_pStrategy = new LazyStrategy();
        }
    }
    CFileWriter() = delete;
    ~CFileWriter()
    {
        if(m_pStrategy != nullptr){
            delete m_pStrategy;
        }
    }
    CFileWriter(const CFileWriter&) = delete;
    CFileWriter& operator=(const CFileWriter&) = delete;
    CFileWriter(const CFileWriter&&) = delete;
    CFileWriter& operator=(const CFileWriter&&) = delete;
    // IWrite Interface
    int WriteAtBegin(void* data,int length) override{
        m_pStrategy->OnWriteAt(0,data,length);
        std::cout << "write" << length << " byte at file begin" << std::endl;
        return length;
    }

    int WriteAt(int pos,void* data,int length) override{
        std::cout << "write" << length << " byte at " << pos << std::endl;
        return length;
    }

    int WriteEnd(void* data,int length) override{
        std::cout << "write" << length << " byte at file end" << std::endl;
        return length;
    }
private:
    std::string m_filename;
    IWriteStrategy * m_pStrategy = nullptr;

};